﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Security;
using System.Security.Policy;
using System.IO;
using System.Diagnostics;

namespace CustomInstallActions
{
    [RunInstaller(true)]
    public partial class MyInstaller : Installer
    {
        public MyInstaller()
        {
            InitializeComponent();
        }
        
        public override void Install(IDictionary stateSaver)
        {
            //***************************************
            //The prior versions do not delete Biolit folder, so these lines of code are necessary
            string BiolitDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Biolit");
            DeleteDirIFExists(BiolitDataDir);
            string BiolitNewDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"BiolitNew");
            DeleteDirIFExists(BiolitNewDataDir);
            string BiolitTechPreviewDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Biolit_TechPreview_Dec_2009");
            DeleteDirIFExists(BiolitTechPreviewDataDir);
            //***************************************
            PolicyLevel ent;
            PolicyLevel mach;
            PolicyLevel user;
            string sAssemblyPath = this.Context.Parameters["custassembly"];
            IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();
            policyEnumerator.MoveNext();
            ent = policyEnumerator.Current as PolicyLevel;

            policyEnumerator.MoveNext();
            mach = policyEnumerator.Current as PolicyLevel;
            
            policyEnumerator.MoveNext();
            user = policyEnumerator.Current as PolicyLevel;

            PermissionSet fullTrust = user.GetNamedPermissionSet("FullTrust");
            PolicyStatement policy = new PolicyStatement(fullTrust, PolicyStatementAttribute.Nothing);
            UrlMembershipCondition condition = new UrlMembershipCondition(sAssemblyPath);
            CodeGroup group = new UnionCodeGroup(condition, policy);
            group.Name = "UCSD_BioLit";
            user.RootCodeGroup.AddChild(group);
            SecurityManager.SavePolicy();
            base.Install(stateSaver);
        }

        private void DeleteFileIFExists(string BiolitDataDir)
        {
            if (true == File.Exists(BiolitDataDir))
            {
                File.Delete(BiolitDataDir);
            }
        }
        private void DeleteDirIFExists(string BiolitDataDir)
        {
                if (true == Directory.Exists(BiolitDataDir))
                {
                    Directory.Delete(BiolitDataDir, true);
                }
        }
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }

        protected override void OnCommitted(IDictionary savedState)
        {
            //static string programFilesLocation = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            
            // We do not need to use the exe to install the Windows Installer since it is a part of the prerequisites.
            //string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Word Add in For Ontology Recognition" + @"\PrerequisiteInstaller.exe");
            //if (File.Exists(filePath))
            //{
            //    Process.Start(filePath);
            //}
            base.OnCommitted(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            string BiolitNewDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"BiolitNew");
            DeleteDirIFExists(BiolitNewDataDir);
            string BiolitTechPreviewDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Biolit_TechPreview_Dec_2009");
            DeleteDirIFExists(BiolitTechPreviewDataDir);
            base.Uninstall(savedState);
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

    }
}
